#include "LoadCP.h"
#include <map>
#include <fstream>
#include <iostream>


using namespace std;

std::map<unsigned long long, unsigned long long> LoadCP::cpMap;
LoadCP::LoadCP()
{
}

LoadCP::~LoadCP()
{
}

void LoadCP::loadCPMap(ifstream* in)
{
	if(in->is_open())
	{
		unsigned long long int word;
		unsigned long long int offset;
		while(!in->eof())
		{
			*in >> word;
			*in >> offset;
			cpMap[word] = offset;
		}
	}
}

unsigned long long LoadCP::getOffset(uint word, int step)
{
	//cout << "Word: " << word << endl;
	//cout << "Step: " << step << endl;
	unsigned long long close=0;
	close = word - word%step;
	//cout << "Close1 = " << close << endl;
	//cout << "Tamanho do Mapa: " << cpMap.size() << endl;
	while(cpMap.find(close)==cpMap.end() && close>=step)
	{
		//cout << "Close = " << close << endl;
		close = close - step;
	}
	//cout << "Close2 = " << close << endl;
	//if(close < step)
	//	return cpMap[step];
	unsigned long long result = cpMap[close];
	//cout << "Estou retornando: " << cpMap[close] << endl;
	return result;
}
